package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.MyEvaluationBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyEvaluationAdapter extends RecyclerArrayAdapter<MyEvaluationBean.DataBean.ListBean> {

    public interface onMyEvaluationInterFace {
        void delete(int position);
    }

    private onMyEvaluationInterFace onMyEvaluationInterFace;

    public void setOnMyEvaluationInterFace(onMyEvaluationInterFace listener) {
        onMyEvaluationInterFace = listener;

    }


    public MyEvaluationAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyEvaluationHolder(parent);
    }


    class MyEvaluationHolder extends BaseViewHolder<MyEvaluationBean.DataBean.ListBean> {


        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_username)
        TextView tvUsername;
        @Bind(R.id.tv_evaluation_time)
        TextView tvEvaluationTime;
        @Bind(R.id.img_del)
        ImageView imgDel;
        @Bind(R.id.tv_reply)
        TextView tvReply;
        @Bind(R.id.reply_layout)
        LinearLayout replyLayout;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_title2)
        TextView tvTitle2;
        @Bind(R.id.tv_look)
        TextView tvLook;
        @Bind(R.id.tv_collection)
        TextView tvCollection;
        @Bind(R.id.tv_zan)
        TextView tvZan;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.ll_book)
        LinearLayout llBook;
        @Bind(R.id.rowFG)
        LinearLayout rowFG;
        @Bind(R.id.img_type)
        TextView imgType;
        @Bind(R.id.tv_commit_name)
        TextView tvCommitName;
        @Bind(R.id.img_type1)
        TextView imgType1;

        public MyEvaluationHolder(ViewGroup parent) {
            super(parent, R.layout.item_my_evaluation);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(MyEvaluationBean.DataBean.ListBean data) {
            super.setData(data);


            if (data.type == 1) {
                imgDel.setVisibility(View.VISIBLE);
                replyLayout.setVisibility(View.GONE);
                imgType1.setVisibility(View.VISIBLE);
                tvCommitName.setVisibility(View.GONE);

            } else if (data.type == 2) {
                imgDel.setVisibility(View.VISIBLE);
                replyLayout.setVisibility(View.VISIBLE);
                imgType.setText("我的回复");
                imgType1.setVisibility(View.GONE);
                tvCommitName.setVisibility(View.VISIBLE);

            } else if (data.type == 3){
                imgDel.setVisibility(View.GONE);
                replyLayout.setVisibility(View.VISIBLE);
                imgType.setText("回复我的");
                imgType1.setVisibility(View.GONE);
                tvCommitName.setVisibility(View.VISIBLE);
            }
            Glide.with(BaseActivity.context)
                    .load(data.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvUsername.setText(data.nickname);
            tvEvaluationTime.setText(data.addtime);

            if (data.type == 1) {
                tvCommitName.setText(data.nickname + ":");
                tvContent.setText(data.content);
            } else if (data.type == 2) {
                tvReply.setText(data.content);
                tvCommitName.setText(data.comment.nickname + ":");
                tvContent.setText(data.comment.content);
            } else if (data.type == 3){
                tvReply.setText(data.content);
                tvCommitName.setText(data.comment.nickname + ":");
                tvContent.setText(data.comment.content);
            }

            if (data.product!=null&&!TextUtils.isEmpty(data.product.icon)){
                Glide.with(BaseActivity.context)
                        .load(data.product.icon+"")
                        .apply(MyApplication.options)
                        .into(img);
            }
            if (data.product!=null){
                tvTitle.setText(data.product.name);
                tvTitle2.setText("");
                tvLook.setText(data.product.viewed + "");
                tvZan.setText(data.product.likes + "");
                tvCollection.setText(data.product.collection + "");
                tvType.setText(data.product.category + "");
            }



            imgDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onMyEvaluationInterFace.delete(getDataPosition());
                }
            });

        }
    }
}
